package com.epoch.servicecourse.controller;

import bean.vo.ResultInfo;
import com.epoch.servicecourse.bean.StuCourse;
import com.epoch.servicecourse.service.StuCourseService;
import exceptions.ParamsException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * @author mi
 */
@RestController
@Slf4j
public class StuCourseController {
    @Resource
    private StuCourseService stuCourseService;
    /**
     * 收藏学生课程
     * @param map
     */
    @ApiOperation(value = "用户版块")
    @PostMapping("epoch/stu/saveStuCourse")
    public ResultInfo saveStuCourse(@RequestBody HashMap<String,String> map){
        String csId = map.get("csId");
        String courseId = map.get("courseId");

        ResultInfo resultinfo=new ResultInfo();
        try{
            StuCourse stuCourse=new StuCourse();
            stuCourse.setCsId(csId);
            stuCourse.setCourseId(courseId);
            stuCourseService.saveStuCourse(stuCourse);
        }catch(ParamsException p){
            p.printStackTrace();
            resultinfo.setCode(p.getCode());
            resultinfo.setMsg(p.getMsg());
        }catch(Exception e){
            e.printStackTrace();
            resultinfo.setCode(500);
            resultinfo.setMsg("收藏课程失败！请稍后重试");
        }
        return resultinfo;
    }
    /**
     * 删除学生课程
     * @param csId
     * @param courseId
     */
    @ApiOperation(value = "用户版块")
    @DeleteMapping("epoch/stu/deleteStuCourse/{csId}/{courseId}")
    public ResultInfo deleteStuCourse(@PathVariable("csId") String csId,@PathVariable("courseId") String courseId){
        ResultInfo resultinfo=new ResultInfo();
        try{
            StuCourse stuCourse=new StuCourse();
            stuCourse.setCsId(csId);
            stuCourse.setCourseId(courseId);
            stuCourseService.deleteStuCourse(stuCourse);
        }catch(ParamsException p){
            p.printStackTrace();
            resultinfo.setCode(p.getCode());
            resultinfo.setMsg(p.getMsg());
        }catch(Exception e){
            e.printStackTrace();
            resultinfo.setCode(500);
            resultinfo.setMsg("删除课程失败！请稍后重试");
        }
        return resultinfo;
    }
    /**
     * 查询学生课程
     * @param csId
     * @return StuCourse
     */
    @ApiOperation(value = "用户版块")
    @PostMapping("epoch/stu/getStuCourseBy")
    public ResultInfo getStuCourseBy(@Valid String csId){
        ResultInfo resultinfo=new ResultInfo();
        try{
            resultinfo.setResult(stuCourseService.getStuCourseById(csId));
        }catch(ParamsException p){
            p.printStackTrace();
            resultinfo.setCode(p.getCode());
            resultinfo.setMsg(p.getMsg());
        }catch(Exception e){
            e.printStackTrace();
            resultinfo.setCode(500);
            resultinfo.setMsg("查询课程失败！请稍后重试");
        }
        return resultinfo;
    }

    @GetMapping("epoch/stu/getFlag/{csId}/{courseId}")
    public ResultInfo getFlag(@PathVariable("csId") String csId,@PathVariable("courseId") String courseId){
        ResultInfo resultinfo=new ResultInfo();
        resultinfo.setResult(stuCourseService.getFlag(csId,courseId));
        return resultinfo;
    }
}
